package mysql

import (
	"backend/app/model"
	"database/sql"
	"errors"
	"fmt"
)

func QueryUserByID(userID int64) (*model.User, error) {
	sqlStr := `select user_id, username, password, status from users where user_id=?`
	var muser model.User
	err := Db.Get(&muser, sqlStr, userID)
	if err == sql.ErrNoRows {
		return &muser, errors.New("用户不存在")
	}
	if err != nil {
		fmt.Printf("scan failed, err : %v\n", err)
		return &muser, err
	}
	return &muser, err
}

func QueryUserByUsername(username string) (*model.User, error) {
	sqlStr := `select user_id, username, password, status from users where username=?`
	var muser model.User
	err := Db.Get(&muser, sqlStr, username)
	if err == sql.ErrNoRows {
		return &muser, errors.New("用户不存在")
	}
	if err != nil {
		fmt.Printf("scan failed, err : %v\n", err)
		return &muser, err
	}
	return &muser, err
}

func CheckUserIsExistByUsername(username string) (bool, error) {
	sqlStr := `select count(user_id) from users where username = ?`
	var count int
	err := Db.Get(&count, sqlStr, username)
	if err != nil {
		fmt.Printf("scan failed, err: %v\n", err)
		return false, err
	}
	if count > 0 {
		return true, nil
	}
	return false, nil
}

func UpdateUser(user *model.User) error {
	sqlStr := `update users set username=?, password=?, status=? where user_id=?`
	_, err := Db.Exec(sqlStr, user.UserName, user.PassWord, user.Status, user.UserID)
	return err
}

func InsertUser(user *model.User) error {
	sqlStr := "insert into users(user_id, username, password, status) values(?,?,?,?)"
	_, err := Db.Exec(sqlStr, user.UserID, user.UserName, user.PassWord, user.Status)
	return err
}

func DeleteUser(userID int64) error {
	sqlStr := `delete from users where user_id=?`
	_, err := Db.Exec(sqlStr, userID)
	return err
}

func QueryAllUserByPage(page, pagesize int64) (*[]model.User, error) {
	sqlStr := `select user_id, username, password, status from users order by id limit ? offset ?`
	var userslist []model.User
	err := Db.Select(&userslist, sqlStr, pagesize, pagesize*(page-1))
	// 循环读取结果集中的数据
	if err != nil {
		return nil, err
	}
	return &userslist, nil
}

func QueryAllUserCount() (int64, error) {
	sqlStr := `select count(user_id) from users`
	var count int64
	err := Db.Get(&count, sqlStr)
	if err != nil {
		return 0, err
	}
	return count, nil
}

func InsertUserInfo(userInfo *model.UserInfo) error {
	sqlStr := "insert into userinfo(user_id, role_name, real_name, avatar, power) values(?,?,?,?,?)"
	_, err := Db.Exec(sqlStr, userInfo.UserID, userInfo.RoleName, userInfo.RealName, userInfo.Avatar, userInfo.Power)
	return err
}

func UpdateUserInfo(userInfo *model.UserInfo) error {
	sqlStr := `update userinfo set role_name=?, real_name=?, avatar=?, power=? where user_id=?`
	_, err := Db.Exec(sqlStr, userInfo.RoleName, userInfo.RealName, userInfo.Avatar, userInfo.Power, userInfo.UserID)
	return err
}

func DeleteUserInfo(userID int64) error {
	sqlStr := `delete from userinfo where user_id=?`
	_, err := Db.Exec(sqlStr, userID)
	return err
}

func CheckUserInfoIsExist(userID int64) (bool, error) {
	sqlStr := `select count(user_id) from userinfo where user_id = ?`
	var count int
	err := Db.Get(&count, sqlStr, userID)
	if err != nil {
		fmt.Printf("scan failed, err: %v\n", err)
		return false, err
	}
	if count > 0 {
		return true, nil
	}
	return false, nil
}

func QueryUserInfo(userID int64) (*model.UserInfo, error) {
	sqlStr := `select user_id, role_name, real_name, avatar, power from userinfo where user_id=?`
	var userinfo model.UserInfo
	err := Db.Get(&userinfo, sqlStr, userID)
	if err == sql.ErrNoRows {
		return &userinfo, errors.New("userinfo is nil")
	}
	if err != nil {
		fmt.Printf("scan failed, err : %v\n", err)
		return &userinfo, err
	}
	return &userinfo, err
}

func QueryUserinfosByRoleName(rolename string) (*[]model.UserInfo, error) {
	rolename = "%" + rolename + "%"
	sqlStr := `select user_id, role_name, real_name, avatar, power from userinfo where role_name like ?`
	var userinfos []model.UserInfo
	err := Db.Select(&userinfos, sqlStr, rolename)
	if err != nil {
		return nil, err
	}
	return &userinfos, err
}
